<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 房间模型类
 * Created by PhpStorm.
 * User: TK
 * Date: 14-8-1
 * Time: 上午11:09
 */
class Order_orderhotelgoods_model extends CI_Model {


    private $table_order_hotel_goods;
    private $table_hotel_goods;
    private $info_table;
    private $sid;


    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->info_table = $this->db->dbprefix("order_info");
        $this->table_order_hotel_goods = $this->db->dbprefix("order_hotel_goods");
        $this->table_hotel_goods = $this->db->dbprefix("hotel_goods");
    }



    /**
     * 根据条件获取数据
     * @param mixed $where 查询条件
     * @param string $field 获取字段
     * @return array $data 返回数据
     */
    public function get_list($where='',$field='*'){

        $this->db->select($field);
        if(!empty($where))$this->db->where($where, NULL, FALSE);
        $this->db->order_by('id', 'DESC');
        $query = $this->db->get($this->table_order_hotel_goods);
        return $query->result_array();
    }


    /**
     * 根据条件获取订单商品
     * @param mixed $where 查询条件
     * @param string $field 获取字段
     * @return array $data 返回数据
     */
    public function get_all_list($where='',$field='*'){

        $this->db->select($field);
        if(!empty($where))$this->db->where($where, NULL, FALSE);
        $this->db->join($this->table_hotel_goods, "$this->table_hotel_goods.id = $this->table_order_hotel_goods.good_id");

        $query = $this->db->get($this->table_order_hotel_goods);
        return $query->result_array();
    }


    /**
     * 更新数据
     * @param array $data  数组
     * @param mixed $where 查询条件 array|string
     * @return boolean 返回数据 true/false
     */
    public function edit_row($data,$where){
        if(!empty($where)) $this->db->where($where, NULL, FALSE);
        return $this->db->update($this->table_order_hotel_goods,$data);
    }



    /**
     * 添加数据
     * @param array $data 数组
     * @return mixed 返回数据 false/[>0]
     */
    public function add_row($data){
        if(is_array($data)){
            if($this->db->insert($this->table_order_hotel_goods,$data)){
                return $this->db->insert_id();
            }else{
                return false;
            }
        }
        return false;
    }

    /**
     * 批量添加数据
     * @param array $data 数组
     * @return mixed 返回数据 false/[>0]
     */
    public function add_batch($data){
        if(is_array($data)){
            if($this->db->insert_batch($this->table_order_hotel_goods,$data,'')){
                return true;
            }
            return false;

        }
        return false;
    }


    /**
     * 批量更新数据
     * @param array $data 数组
     * @return mixed 返回数据 false/[>0]
     */
    public function update_batch($data){



        if(!empty($data)){

            //判断是否存在数据
            $old_result = $this->get_list(array('order_id'=>$data[0]['order_id']));


            $old_data = array();
            if($old_result)
            {
                foreach ($old_result as $index => $item)
                {
                    $old_data[$item['good_id']] = array(
                        'sid' => $item['sid'] ,
                        'order_id' => $item['order_id'],
                        'good_id' => $item['good_id'],
                        'number' => $item['number']
                    );
                }
            }

            if(CSADMIN)
            {
                print_r($old_data);
                print_r($old_data['117']);

            }


            if($old_data){

                $insert_data = array();

                foreach($data as $key=>$val){

                    //不存在原先数据时 添加数据
                    if (!isset($old_data[$val['good_id']]))
                    {
                        $insert_data[] = array(
                            'sid' => $val['sid'],
                            'good_id' => $val['good_id'],
                            'order_id' => $val['good_id'],
                            'number' => $val['number']
                        );
                        continue;
                    }
                    else
                    {

                        /* if(TADMIN)
                         {
                             $diff = array_diff($old_data[$val['good_id']], $val);
                             if(diff)
                             {
                                 echo '更新';
                             }

                         }

                         if (array_diff($old_data[$val['good_id']], $val))
                         {*/




                    $this->edit_row(array('number'=>$val['number']), array('order_id' => $val['order_id'], 'good_id' => $val['good_id']));





//                        }

                    }



                }



                if(!empty($insert_data)){
                    return $this->db->insert_batch($this->table_order_hotel_goods, $insert_data);
                }


                return true;
            }else{
                //添加数据
                $insert_data = array();
                foreach($data as $key=>$val){
                    $insert_data[] = array(
                        'sid' => $val['sid'],
                        'good_id' => $val['good_id'],
                        'order_id' => $val['order_id'],
                        'number' => $val['number']
                    );
                }
                

                return $this->db->insert_batch($this->table_order_hotel_goods, $insert_data);
            }
        }

        return false;
    }





    /**
     * 查询单条记录
     * @param mixed $where 查询条件 array|string
     * @param string $field 获取到的字段
     * @return mixed 返回数据
     */
    public function get_row($where,$field='*')
    {
        if(!empty($where)) $this->db->where($where, NULL, FALSE);
        $query = $this->db->get($this->table_order_hotel_goods,1);
        return $query->row_array();
    }

    /**
     * 删除内容
     * @param mixed $where 查询条件 array|string
     * @return bool 返回数据
     */
    public function delete_row($where)
    {
        if(!empty($where))return $this->db->delete($this->table_order_hotel_goods,$where);
        return false;
    }



}

